﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;
using Gift.DAL;
using Gift.Model;

namespace WebSite.cn
{
    public partial class index : BasePage
    {
        public StringBuilder sbFlashHtml = new StringBuilder();
        public StringBuilder sbProfessionHtml = new StringBuilder();
        public StringBuilder sProductHtml = new StringBuilder();
        public StringBuilder sMarketProductHtml = new StringBuilder();
        public StringBuilder sRecommGoodsHtml = new StringBuilder();
        public StringBuilder sBrandHtml = new StringBuilder();
        public StringBuilder sBrandPicHtml = new StringBuilder();
             public string imgLink = "";
             public string imgNumber = "";

        DALProfessionAreaType theAreaType = new DALProfessionAreaType();
        DALGoods theGoods = new DALGoods();
        readonly DALBrand theBrand = new DALBrand();

        StringBuilder _newsFudian = new StringBuilder();


        string sBodyAd1 = "";

        public string SBodyAd1
        {
            get { return sBodyAd1; }
        }
        string sBodyAd2 = "";

        public string SBodyAd2
        {
            get { return sBodyAd2; }
        }
        string sBodyAd3 = "";

        public string SBodyAd3
        {
            get { return sBodyAd3; }
        }
        string sBodyAd4 = "";

        public string SBodyAd4
        {
            get { return sBodyAd4; }
        }


        public StringBuilder NewsFudian
        {
          get { return _newsFudian; }

        }

        StringBuilder _newsLawsRule = new StringBuilder();

        public StringBuilder NewsLawsRule
        {
          get { return _newsLawsRule; }

        }



        // Methods
        private void Bind()
        {
            DataTable table = SQLAccess.GreatDs("select isnull(flashurl,'') as flashurl,isnull(flashname,'') as flashname,isnull(flashpic,'') as flashpic from DB_Flash where isnull(classid,0) = 0 order by flashid asc").Tables[0];

            this.imgLink = "";
            this.imgNumber = "";
      
                string sFlashUrl = string.Empty;
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    if (i < table.Rows.Count)
                    {
                        if (table.Rows[i]["flashurl"] == null || table.Rows[i]["flashurl"].ToString() == "")
                        {
                            sFlashUrl = "#";
                        }
                        else
                        {
                            sFlashUrl = table.Rows[i]["flashurl"].ToString();
                        }

                        this.imgLink = this.imgLink + " <li><a href='" + sFlashUrl + "'><img src='../uploadfile/" + table.Rows[i]["flashpic"].ToString().Replace(@"\", "/") + "' width='1200px' height='325px'/></a></li>";
                        this.imgNumber = this.imgNumber + "<li>" + (i + 1).ToString() + "</li>";
                    }
                }
                    //else if (i == (table.Rows.Count - 1))
                    //{
                    //    this.imgLink = this.imgLink + " <li><a href='" + "../uploadfile/" + table.Rows[i]["flashpic"].ToString().Replace(@"\", "/") + "'><img src='" + table.Rows[i]["flashurl"].ToString() + "'/></a></li>";
                    //    this.imgNumber = this.imgNumber + "<li>" + (i+1).ToString() + "</li>"; 
                    //}
                

          //  sbFlashHtml.AppendLine("  ");
            //    foreach (DataRow dr in table.Rows)
            //    {
            //        sbFlashHtml.AppendFormat(" <a href=\"{0}\" target=\"_blank\"><img width=\"1200px\" height=\"325px\" src=\"{1}\" alt=\"{2}\" /></a>"
            //                                    , dr["flashurl"].ToString()
            //                                , "/uploadfile/" + dr["flashpic"].ToString().Replace(@"\", "/")
            //                                , dr["flashname"].ToString()
            //                                );


             
             
          
            //}
               // sbFlashHtml.AppendLine("</div>");
                table = SQLAccess.GreatDs(string.Concat(new object[] { "select top 10 newsId,newsname,typeid,updateTime from tbNews where typeid in(", "select typeId from tbNewsType where typeName='福点动态'", ") and visible=1 and isnew=1 order by newsorder,updatetime desc" })).Tables[0];
                if (table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        _newsFudian.AppendFormat("<li><a href=\"{0}\" target='_blank' title=\"{1}\">{2}</a><div style='float:right;color:#207D38;'></div></li>"
                                                 , "/cn/news_01.aspx?tid=" + row["typeid"].ToString() + "&nid=" + row["newsid"].ToString()
                                                 , row["newsname"].ToString()
                                                 , row["newsname"].ToString() // fun.Left(row["newsname"].ToString(), 15) + ".."
                                                 );
                    }
                }

                table = SQLAccess.GreatDs(string.Concat(new object[] { "select top 10 newsid,newsname,typeid,updateTime from tbNews where typeid in(", "select typeId from tbNewsType where typeName='政策法规'", ") and visible=1 and isnew=1 order by newsorder,updatetime desc" })).Tables[0];
                if (table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        _newsLawsRule.AppendFormat("<li><a href=\"{0}\" target='_blank' title=\"{1}\">{2}</a><div style='float:right;color:#207D38;'></div></li>"
                                                , "/cn/news_01.aspx?tid=" + row["typeid"].ToString() + "&nid=" + row["newsid"].ToString()
                                                , row["newsname"].ToString()
                                                , row["newsname"].ToString() //fun.Left(row["newsname"].ToString(), 15) + ".."
                                                );
                    }
                }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["backHref"] = Request.Url.PathAndQuery;
            if (!base.IsPostBack)
            {
                this.Bind();
                //this.BindBodyAd();
                this.BindBrand();
                this.BindRecommGoods();
            }
        }

        void BindBrand()
        {
          
            DataTable table = theBrand.GetList(" isnull(visible,0)=1 and brandName !='其它' order by brandOrder ").Tables[0];
            int iRowIndex = 0;
            foreach(DataRow dr in table.Rows)
            {
                if (dr["brandLogo"].ToString() != "")
                {
                    if (iRowIndex < 10)
                    {
                        sBrandHtml.AppendFormat("<A href=\"/cn/hotpro.aspx?bid={0}\">{1}</A><EM>|</EM>"
                                                , dr["brandId"]
                                                , dr["brandName"]
                                                );
                    }

                    sBrandPicHtml.AppendFormat("<li><a href=\"/cn/hotpro.aspx?bid={0}\" target=\"_blank\" title=\"{1}\"><img src=\"{2}\"></a></li>"
                                               , dr["brandId"]
                                               , dr["brandName"]
                                               , "/uploadfile/product/" + dr["brandLogo"]);
                    iRowIndex++;
                }
            }
         
            //if (sBrandHtml.Length > 10)
            //{
            //    sBrandHtml.Remove(sBrandHtml.Length - 10, 10);
            //}
        }



        void BindBodyAd()
        {
            string str3 = "select topicid,pic,picurl,visible from DB_topic where isnew=1 order by topicorder asc";
           DataTable table3 = SQLAccess.GreatDs(str3).Tables[0];
            if (table3.Rows.Count > 1 && table3.Rows[1]["visible"].ToString().ToLower() == "true")
            {
                this.sBodyAd1 = "<div class=\"banner_ad\"><a href='" + table3.Rows[1]["picurl"].ToString()  + "' target='_blank'><img src='" + table3.Rows[1]["pic"].ToString() + "' border='0' /></a></div>";
            }
            if (table3.Rows.Count > 2 && table3.Rows[2]["visible"].ToString().ToLower() == "true")
            {
                this.sBodyAd2 = "<div class=\"banner_ad\"><a href='" + table3.Rows[2]["picurl"].ToString()  + "' target='_blank'><img  src='" + table3.Rows[2]["pic"].ToString() + "' border='0' /></a></div>";
            }
            if (table3.Rows.Count > 3 && table3.Rows[3]["visible"].ToString().ToLower() == "true")
            {
                this.sBodyAd3 = "<div class=\"banner_ad\"><a href='" + table3.Rows[3]["picurl"].ToString() + "' target='_blank'><img  src='" + table3.Rows[3]["pic"].ToString() + "' border='0' /></a></div>";
            }
            if (table3.Rows.Count > 4 && table3.Rows[4]["visible"].ToString().ToLower() == "true")
            {
                this.sBodyAd4 = "<div class=\"banner_ad\"><a href='" + table3.Rows[4]["picurl"].ToString()  + "' target='_blank'><img  src='" + table3.Rows[4]["pic"].ToString() + "' border='0' /></a></div>";
            }
        }

      void   BindRecommGoods()
        {
            sRecommGoodsHtml = new StringBuilder();
            DataTable dtGoods = theGoods.GetList(" isnull(visible,0)=1 and isnull(isSale,0)=1 and isnull(isRecom,0)=1  order by goodsOrder ").Tables[0];

          sRecommGoodsHtml.AppendLine("<textarea class=\"lazyload\">");
                sRecommGoodsHtml.Append("<div class=\"info_list\"><ul>");
                string sProName = string.Empty;
                foreach (DataRow drSub in dtGoods.Rows)
                {
                    sProName = drSub["goodsName"].ToString().Length > 12 ? drSub["goodsName"].ToString().Substring(0, 12) + ".." : drSub["goodsName"].ToString();
                    sRecommGoodsHtml.AppendFormat("<li>"
                                    + " <a href=\"{0}\" target=\"_self\" name=\"#hoverinfo{1}\" class=\"pro_img\"><img alt=\"\" src=\"{2}\" /></a>"
                                    + " <p><a href=\"{0}\" target=\"_blank\" title=\"{6}\">{3}</a><em>兑换价：{4}福点</em></p>"
                                    + " <div id=\"hoverinfo{1}\" style=\"display: none;\"><img width=\"200\" height=\"200\" src=\"{2}\" alt=\"{6}\"/><p><strong>{6}</strong> <span>商品编号:{5}</span> <span><em>库存：{4}</em></span><span><em>兑换价：{5}福点</em></span></p></div>"
                                    + "</li>"
                                    , "/cn/goodsDetail.html?gid=" + drSub["goodsId"].ToString()
                                    , drSub["goodsId"].ToString()
                                    , "/uploadfile/product/" + drSub["smallPic"].ToString()
                                    , sProName
                                    ,  drSub["changePoints"].ToString()
                                    , drSub["goodsCode"].ToString()
                                    , drSub["goodsName"].ToString()
                                    );
                }

                sRecommGoodsHtml.Append("</ul><div class=\"clear\"></div></div><script type=\"text/javascript\">    $(function () { $(\"a.pro_img\").tooltip({ bodyHandler: function () { return $($(this).attr(\"name\")).html(); }, showURL: false }); });</script>");
              sRecommGoodsHtml.AppendLine("</textarea>");
              sRecommGoodsHtml.AppendLine(" <div style=\"height:203px;\" class=\"div_lazyload_32\"></div>");

        }

    }
}